let express = require('express');
let router = express.Router();
var multer = require('../../components/multerUtil');
let filter = require('../../components/filter');

/**
 * 查询视频数据 by v 2018-06-29
 *
 * subjectId为空查询所有，否则查询指定subjectId下的视频数据
 */
router.get('/', filter.authorize, function (req, res, next) {
    let params = req.query;

    var limits = "";
    var sql = "";
    var datas = [];

    if (params.subjectId !== undefined && params.subjectId !== "") {
        sql = " where subjectId = ?";
        datas.push(params.subjectId);
    }

    req.app.db.query("select count(1) as totalCount from media " + sql, datas, function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results[0].totalCount === 0) {
            res.json({"success": true, "data": {}, "totalCount": 0});
            return;
        }

        let totalCount = results[0].totalCount;

        if (params.pageNo > 0 && params.pageSize > 0) {
            limits = " limit ?, ?";
            datas.push((params.pageNo - 1) * params.pageSize);
            datas.push(params.pageSize * 1);
        }

        req.app.db.query("select * from media " + sql + limits, datas, function (err, results, fields) {
            if (err) {
                res.json({"success": false, "message": global.databaseErr, "detail": err});
                return;
            }

            for (var i = 0; i < results.length; i++) {
                if (results[i].mediaFile !== "") {
                    results[i].mediaFile = global.contextPath + "/download/" + results[i].mediaFile;
                }
                results[i].uploadFile = {fileArray: []};
            }

            res.json({"success": true, "data": results, "totalCount": totalCount});
        });
    });
});

/**
 * 查看视频数据 by p 2018-06-11
 *
 * 按id查看指定的视频信息
 */
router.get('/:id', filter.authorize, function (req, res, next) {
    let params = req.params;

    req.app.db.query("select * from media where id = ?", [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results.length === 0) {
            res.json({"success": false, "message": global.noDataErr});
            return;
        }

        if (results[0].mediaFile !== "") {
            results[0].mediaFile = global.contextPath + "/download/" + results[0].mediaFile;
        }
        res.json({"success": true, "data": results[0]});
    });
});

/**
 * 修改视频数据 by p 2018-06-29
 *
 *
 */
router.put('/:id', filter.authorize, multer.single("uploadFile"), function (req, res, next) {
    let params = req.body;
    var datas = [];

    var title = "";
    var mediaFile = "";

    if (params.id === undefined || params.id === '' || params.id <= 0) {
        res.json({"success": false, "message": "视频编码不正确！"});
        return;
    }

    if (params.subjectId === undefined || params.subjectId === '') {
        res.json({"success": false, "message": "所属章节编码不正确！"});
        return;
    }
    datas.push(params.subjectId);

    if (params.title !== undefined && params.title !== '') {
        title = params.title;
    }
    datas.push(title);

    if (req.file !== undefined) {
        mediaFile = req.file.filename;
        datas.push(mediaFile);
    }

    datas.push(params.id);

    req.app.db.query("update media set subjectId = ?, title = ?" + (mediaFile === "" ? "" : ", mediaFile = ?") + " where id = ?", datas, function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.putMsg});
    });
});

/**
 * 新增视频数据 by p 2018-06-11
 *
 *
 */
router.post('/', filter.authorize, multer.single("uploadFile"), function (req, res, next) {
    let params = req.body;

    var title = "";
    var mediaFile = "";

    if (params.subjectId === undefined || params.subjectId === '') {
        res.json({"success": false, "message": "所属章节编码不正确！"});
        return;
    }

    if (params.title !== undefined && params.title !== '') {
        title = params.title;
    }

    if (req.file !== undefined) {
        mediaFile = req.file.filename;
    }

    req.app.db.query("insert into media(subjectId, title, mediaFile) values(?, ?, ?)", [params.subjectId, title, mediaFile], function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.postMsg});
    });
});

/**
 * 删除视频信息 by v 2018-06-11
 *
 * 按id删除指定的视频信息
 */
router.delete('/:id', function (req, res, next) {
    let params = req.params;

    req.app.db.query('delete from media where id = ?', [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.deleteMsg});
    });
});

module.exports = router;